package com.hp.bon.sgw.wh.utils;

import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.wh.bean.tmall.TaobaoPayRequestBean;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.simpleframework.http.Form;
import org.simpleframework.http.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map.Entry;
import java.util.TreeMap;
/**
 * 
 * 说   明：SGW固话宽带充值 WS请求 临时参数对象 单元处理类
 * 描   述：SGW固话宽带充值 WS请求 临时参数对象 单元处理类
 * @author C.J
 * @date 2017年12月4日 上午11:00:03
 */
public class TaobaoPayRequestUtil {
	private static final Logger _log = LoggerFactory.getLogger(TaobaoPayRequestUtil.class);
	/**
	 * 
	  * 
	  * @Description:设置临时参数对象
	  * @param :TODO
	  * @return:TaobaoPayRequestBean 临时参数对象
	 */ 
	public static TaobaoPayRequestBean getReqParams(Request request){
		TaobaoPayRequestBean bean = new TaobaoPayRequestBean();
		//bizType值为ECARD_FLOWCHARGE
		try {
			String tbOrderNo = request.getParameter("tbOrderNo") == null ? ""
					: request.getParameter("tbOrderNo");
			String bizType = request.getParameter("bizType") == null ? ""
					: request.getParameter("bizType");
			String spuId = request.getParameter("spuId") == null ? ""
					: request.getParameter("spuId");
			String accountVal = request.getParameter("accountVal") == null ? ""
					: request.getParameter("accountVal");
			String supplierId = request.getParameter("supplierId") == null ? ""
					: request.getParameter("supplierId");
			
			String timeStart = request.getParameter("timeStart") == null ? ""
					: request.getParameter("timeStart");
			String timeLimit = request.getParameter("timeLimit") == null ? ""
					: request.getParameter("timeLimit");
			String buyNum = request.getParameter("buyNum") == null ? ""
					: request.getParameter("buyNum");
			String unitPrice = request.getParameter("unitPrice") == null ? ""
					: request.getParameter("unitPrice");
			String totalPrice = request.getParameter("totalPrice") == null ? ""
					: request.getParameter("totalPrice");
			
			String tbPrice = request.getParameter("tbPrice") == null ? ""
					: request.getParameter("tbPrice");
			String buyerId = request.getParameter("buyerId") == null ? ""
					: request.getParameter("buyerId");
			String sellerId = request.getParameter("sellerId") == null ? ""
					: request.getParameter("sellerId");
			String buyerIp = request.getParameter("buyerIp") == null ? ""
					: request.getParameter("buyerIp");
			String notifyUrl = request.getParameter("notifyUrl") == null ? ""
					: request.getParameter("notifyUrl");
			
			String ts = request.getParameter("ts") == null ? ""
					: request.getParameter("ts");
			String sign = request.getParameter("sign") == null ? ""
					: request.getParameter("sign");
			
			bean.setTbOrderNo(tbOrderNo);
			bean.setBizType(bizType);
			bean.setSpuId(spuId);
			bean.setAccountVal(accountVal);
			bean.setSupplierId(supplierId);
			
			bean.setTimeStart(timeStart);
			bean.setTimeLimit(timeLimit);
			bean.setBuyNum(buyNum);
			bean.setUnitPrice(unitPrice);
			bean.setTotalPrice(totalPrice);
			
			bean.setTbPrice(tbPrice);
			bean.setBuyerId(buyerId);
			bean.setSellerId(sellerId);
			bean.setBuyerIp(buyerIp);
			bean.setNotifyUrl(notifyUrl);
			
			bean.setTs(ts);
			bean.setSign(sign);
		} catch (IOException e) {
			_log.error("getReqParams IOException e:"+e.getMessage());
		}
		return bean;
	}

	public static Boolean checkReqWithNotEmpty(TaobaoPayRequestBean bean){
		Boolean bln = true;
		if ( bean == null || "".equals(bean.getBizType()) || "".equals(bean.getTbOrderNo()) ||
			 "".equals(bean.getSupplierId()) || "".equals(bean.getTs()) ||
			 "".equals(bean.getSign()) || "".equals(bean.getSpuId()) ||
			 "".equals(bean.getAccountVal()) || "".equals(bean.getTimeStart()) ||
			 "".equals(bean.getTimeLimit()) || "".equals(bean.getBuyNum()) ||
			 "".equals(bean.getUnitPrice()) || "".equals(bean.getTotalPrice()) ||	
			  "".equals(bean.getBuyerId()) ||
			 "".equals(bean.getNotifyUrl()) ){
			_log.error("checkReqWithNotEmpty is null");
			bln = false;
		}
		return bln;
	}
	
	public static String createSource(Request request){
		String source = "";
		TreeMap<String, String> paraMap = new TreeMap<String, String>();
		Form form = null;
		try {
			form = request.getForm();
			for (Entry<String, String> entry : form.entrySet()) {
				paraMap.put(entry.getKey(), entry.getValue());
			}
			for (Entry<String, String> entry : paraMap.entrySet()) {
				if (!entry.getKey().equals("sign")) {
					source += entry.getKey() + entry.getValue();
				}
			}
		} catch (IOException e) {
			_log.info("createSource error,Msg={}", e.getMessage());
		}
		_log.info("source=" + source);
		return source;
	}
	
	//业务为流量充值时设置请求Req XmlMessage
	public static XmlMessage createReqXmlMessage(String accountVal,String tmallHostId,String sessionId,
			String IP,long receiveTime,String _scid){
		XmlMessage req = new XmlMessage(true);
		Element serviceInformation = DocumentHelper.createElement("Service_Information");
		Element element = serviceInformation.addElement("Acc_Nbr");// Acc_Nbr
		element.setText(accountVal);
		element = serviceInformation.addElement("Destination_Attr");// DestinationAttr
		element.setText("2");
		req.setIp(IP);
		req.setMsgTime(receiveTime);
		req.setMID(sessionId);
		req.setUID(tmallHostId);
		req.setSCID(_scid);
		req.setSIG(null);
		req.setREPTAG(0);
		req.setDATA(serviceInformation.asXML());
		req.setDataElement(serviceInformation);
		_log.info("Data" + req.getDATA().toString());
		return req;
	}

    /**
     * 淘宝充值
     */
	//业务固话宽带充值 为时设置请求Req XmlMessage
	public static XmlMessage createReqXmlMsgNetWork(String accountVal,String tmallHostId,String sessionId,String IP,long receiveTime,
			String _scid,String Area_Code,String taobaoBankCode,String tbOrderNo,String totalPrice,String _payment_Amount){
		XmlMessage req = new XmlMessage(true);
		try{
			Element serviceInformation = DocumentHelper.createElement("Service_Information");
            Element element = serviceInformation.addElement("Channel_Id");
            element.setText("200");
            element = serviceInformation.addElement("Area_Code");
            element.setText(Area_Code);
            element = serviceInformation.addElement("DestinationAttr");
            element.setText("2");
            element = serviceInformation.addElement("Destination_Id");
            element.setText(accountVal);
            element = serviceInformation.addElement("Destination_Id_Type");
            element.setText("2");
            element = serviceInformation.addElement("Payment_Amount");
            element.setText(_payment_Amount);
            element = serviceInformation.addElement("RequestAmount");
            element.setText(totalPrice);
            element = serviceInformation.addElement("Payment_Method");
            element.setText("11");
            element = serviceInformation.addElement("Paid_Time");
            element.setText(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            element = serviceInformation.addElement("Payment_Serial_Nbr");
            element.setText(tbOrderNo);
            element = serviceInformation.addElement("Balance_Type");
            element.setText("0");
            element = serviceInformation.addElement("Bank_code");
            element.setText(taobaoBankCode);
			req.setIp(IP);
			req.setMsgTime(receiveTime);
			req.setMID(sessionId);
			req.setUID(tmallHostId);
			req.setSCID(_scid);
			req.setSIG(null);
			req.setREPTAG(0);
			req.setDATA(serviceInformation.asXML());
			req.setDataElement(serviceInformation);
		}
		catch(Exception e){
			_log.error("createReqXmlMsgNetWork  is error ,Msg= {}",e.getMessage() );
		}
		_log.info("Data=" + req.getDATA().toString());
		return req;
	}

    /**
     * 天猫充值 鉴权
     */
    public static XmlMessage createReqAAA(String accountVal,String tmallHostId,String sessionId,String IP,long receiveTime,
                                                    String _scid,String[] accountVals,String taobaoBankCode,String tbOrderNo,String totalPrice,String _payment_Amount){
        XmlMessage req = new XmlMessage(true);
        try{
            Element serviceInformation = DocumentHelper.createElement("Service_Information");
            Element element = serviceInformation.addElement("Channel_Id");
            element.setText("200");
            element = serviceInformation.addElement("Area_Code");
            element.setText("027");
            element = serviceInformation.addElement("DestinationAttr");
            element.setText("2");
            element = serviceInformation.addElement("Destination_Id");
            element.setText(accountVal);
            element = serviceInformation.addElement("Destination_Id_Type");
            element.setText("2");
            req.setIp(IP);
            req.setMsgTime(receiveTime);
            req.setMID(sessionId);
            req.setUID(tmallHostId);
            req.setSCID(_scid);
            req.setSIG(null);
            req.setREPTAG(0);
            req.setDATA(serviceInformation.asXML());
            req.setDataElement(serviceInformation);
        }
        catch(Exception e){
            _log.error("createReqXmlMsgNetWork  is error ,Msg= {}",e.getMessage() );
        }
        _log.info("Data=" + req.getDATA().toString());
        return req;
    }
	
}
